import {LOGIN_STATUS, EVENT_BUS_KEY} from '../../common/constants';
import {eventBusGetter} from '../../utils/eventbus';
import {routerGetter} from '../../utils/router';
import {getCoureseList} from '../../data-model/model-courese';
import {getIndexAD} from '../../data-model/model-ad';
import {getLoginStatus} from '../../common/service/user-service';

Page({
    data: {
        banners: [],
        interval: 6000,
        tabsData: [],
        tabsFixTop: false,
        coursesList: [],
        subBanners: [],
        subTitle: '',
        audioInfo: {},
        refreshStatus: 0,
        isShowCourese: false,
        isShowBanner: false, // 是否显示顶端banner
        isShowAd: false, // 是否显示广告
        nextId: '',
        lastPage: true,
        currentTagId: 0
    },
    onLoad (e) {
        eventBusGetter().$on(EVENT_BUS_KEY.LOGIN_STATUS_UPDATE, this.refreshPage);
        eventBusGetter().$on(EVENT_BUS_KEY.PAY_COURSE_SUCCESS, this.refreshPage);
        this.refreshPage();
    },
    onShow: function (params) {
        this.setData({
            refreshStatus: new Date().getTime()
        });
    },
    onPullDownRefresh: function (e) {
        // this.refreshPage(LOGIN_STATUS.ZH_LOGIN_SUCCESS);
        this.updateAd();
        this.updateCourese(this.data.currentTagId, '', true);
        wx.stopPullDownRefresh();
    },
    onReachBottom: function (e) {
        if (this.data.nextId && this.data.lastPage !== true) {
            this.updateCourese(this.data.currentTagId, this.data.nextId, false);
        }
    },
    onPageScroll: function (e) {
        let _data = this.data;
        let topHeight = 0;
        let scrollHeight = 0;

        if (_data.banners && _data.banners.length !== 0) {
            topHeight += 220;
        }

        if (typeof _data.subBanners === 'undefined' || _data.subBanners.length >= 2) {
            topHeight += 215;
        }

        let res = wx.getSystemInfoSync();
        scrollHeight = (res.windowWidth / 375.0) * (topHeight);

        if (e.scrollTop > scrollHeight) {
            _data.tabsFixTop === false && this.setData({
                tabsFixTop: true
            });
        } else {
            _data.tabsFixTop === true && this.setData({
                tabsFixTop: false
            });
        }
    },
    updateCourese: function (tagId, nextId, isRefresh) {
        let that = this;
        getCoureseList(tagId, nextId).then((res) => {
            if (isRefresh) {
                let updateData;
                // 下拉刷新赋值
                updateData = {
                    lastPage: res.courses.lastPage,
                    isShowCourese: true,
                    coursesList: res.courses.data
                };
                if (res.courses.nextId) {
                    updateData = Object.assign({nextId: res.courses.nextId}, updateData);
                }
                if (!tagId) {
                    updateData.tabsData = res.tags;
                }
                that.setData(updateData);
            } else {
                let list = that.data.coursesList.concat(res.courses.data);
                that.setData({
                    lastPage: res.courses.lastPage,
                    coursesList: list,
                    nextId: res.courses.nextId ? res.courses.nextId : ''
                });
            }
        }).catch(err => {
            console.log(err);
        });
    },
    selectTab: function (e) {
        this.data.currentTagId = e.detail.tagId;
        console.log(this.data.currentTagId);
        this.data.nextId = '';
        this.data.lastPage = true;
        this.updateCourese(e.detail.tagId, '', true);
    },
    refreshPage: function () {
        if (getLoginStatus() === LOGIN_STATUS.ZH_LOGIN_SUCCESS || getLoginStatus() === LOGIN_STATUS.ZH_LOGIN_FAILED) {
            this.updateAd();
            this.updateCourese(this.data.currentTagId, '', true);
        }
    },
    updateAd () {
        getIndexAD().then(res => {
            let isShowBanner = false;
            let isShowAd = false;

            if (res.banners && res.banners.length > 0) {
                isShowBanner = true;
            }
            if (res.subBanners && res.subBanners.length >= 2) {
                isShowAd = true;
            }

            this.setData({
                banners: res.banners,
                subTitle: res.subTitle || '',
                subBanners: res.subBanners,
                isShowBanner,
                isShowAd
            });
        });
    },
    handleShowAd: function (e) {
        let dataset = e.currentTarget.dataset;
        routerGetter().execute(dataset.uri);
    }
});
